package com.sne.airways.system.utils;

import com.sne.airways.basecore.exceptions.SessionException;
import com.sne.airways.basecore.utils.CommonUtils;
import com.sne.airways.system.acl.Session;
import com.sne.airways.system.constants.Constant;
import com.sne.airways.system.pojo.TdUser;
import com.sne.airways.system.syscache.SessionCache;
import com.sne.airways.system.syscache.SystemPropertiesCache;
import org.springframework.util.StringUtils;

import java.io.Serializable;

/**
 * Created by IntelliJ IDEA.
 * User: skyline
 * Date: 12-8-29
 * Time: 上午10:00
 * To change this template use File | Settings | File Templates.
 */
public class AppUtils extends CommonUtils {
    //用户登录信息属性缓存
    private static SessionCache sessionCache = null;
    //系统属性缓存
    private static SystemPropertiesCache systemPropertiesCache = null;

    /**
     * 获取用户信息session 类
     * @return
     */
    public static Session getSession() {
        return getSessionCache().getSession(getHttpSession().getId());
    }

    /**
     * sessionCache 用来存储用户信息的缓存
     * @return
     */
    public static SessionCache getSessionCache(){
        if(sessionCache == null){
            return getBean(SessionCache.class);
        }
        return sessionCache;
    }

    /**
     * systemPropertiesCache 用来存储系统属性缓存
     * @return
     */
    public static SystemPropertiesCache getSystemPropertiesCache(){
        if(systemPropertiesCache == null){
            return getBean(SystemPropertiesCache.class);
        }
        return systemPropertiesCache;
    }

    /**
     * 把序列化后的数据存入sessionCache
     * @param key
     * @param value
     */
    public static void setSessionCacheAttribute(String key,Serializable value){
        getSessionCache().put(key,value);
    }

    /**
     * 获取当前操作用户
     * @return currentOperator
     */
    public static TdUser getCurrentUser(){
        Object o = null;
        try {
            o = getSession().getUser();
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            o=null;
        }
        if(o == null){
            throw new SessionException("当前用户会话信息已经过期,请重新登录");
        }
        return (TdUser)o;
    }

    public static String getRemoteAddr() {
        String ip = getRequest().getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = getRequest().getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = getRequest().getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = getRequest().getRemoteAddr();
        }
        return ip;
    }

    /**
     * 根据登录名判断用户是不是系统管理员组
     * @param userNo
     * @return
     */
    public static boolean isAdministrator(String userNo) {
        boolean result = false;
        String administratorUsernames = getSystemPropertiesCache().getSystemPropertiesValue(Constant.ADMINISTRATOR_USERNAMES);
        if (StringUtils.hasText(administratorUsernames)) {
            for (String name : administratorUsernames.split(",")) {
                if (userNo.equals(name)) {
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
    /**
     * 判断当前登录用户是不是系统管理员组
     * @return
     */
    public static boolean isAdministrator() {
        return isAdministrator(getCurrentUser().getUserNo());
    }

}
